<template>
  <div>
    <!-- 面包屑导航 -->

    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->

    <el-card>
      <!-- 头部 -->
      <el-row>
        <el-col :span="6">
          <!-- 头部输入框 -->

          <el-input
            placeholder="请输入内容"
            v-model="queryInfo.query"
            clearable
            @clear="getOrderList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="getOrderList()"
            ></el-button>
          </el-input>
        </el-col>
      </el-row>

      <!-- 表格 -->
      <el-table :data="orderList" border stripe>
        <el-table-column type="index" label="#"> </el-table-column>
        <el-table-column prop="order_number" label="订单编号"></el-table-column>
        <el-table-column prop="order_price" label="订单价格"></el-table-column>
        <el-table-column label="是否付款">
          <!-- 1 已付款 0 未付款 -->
          <template slot-scope="scope">
            <el-tag
              type="success"
              v-if="scope.row.pay_status === '1'"
              size="mini"
              >已付款</el-tag
            >
            <el-tag type="success" v-else size="mini">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货"></el-table-column>
        <el-table-column prop="create_time" label="下单时间">
          <template slot-scope="scope">
            {{ scope.row.create_time | formatTime }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="showAddressBox"
            ></el-button>
            <el-tooltip
              class="item"
              effect="dark"
              content="物流信息"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="success"
                size="mini"
                icon="el-icon-location"
                @click="showExpressBox(scope.row)"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->

      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址对话框 -->

    <el-dialog
      title="修改地址"
      :visible.sync="addressBoxVisiable"
      width="30%"
      @close="addressDialogClose"
    >
      <!-- 表单数据 -->
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            v-model="addressForm.city"
            :options="citydata"
            :props="{ expandTrigger: 'hover' }"
            @change="handleChange"
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAdress">
          <el-input v-model="addressForm.detailAdress"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addressBoxVisiable = false">取 消</el-button>
        <el-button type="primary" @click="addressBoxVisiable = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 查看物流信息对话框 -->
    <el-dialog
      title="物流信息"
      :visible.sync="expressDialogVisiable"
      width="30%"
    >
      <!-- 时间轴 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in expressData"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>

    </el-dialog>
  </div>
</template>

<script>
import { getdata } from "../../network/getdata";
import { formatTime } from "../../common/utils/formdate";

// 中国县市区级联数据
import { regionData } from "element-china-area-data";
export default {
  name: "order",
  data() {
    return {
      // 请求参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      //总条数
      total: 0,
      //订单数据列表
      orderList: [],
      // 修改地址对话框显示与隐藏
      addressBoxVisiable: false,
      // 修改地址表单
      addressForm: {
        city: [],
        detailAdress: "",
      },
      //修改地址表单校验规则
      addressFormRules: {
        city: [{ required: true, message: "请选择省市区/县", trigger: "blur" }],
        detailAdress: [
          { required: true, message: "请输入详细地址", trigger: "blur" },
        ],
      },
      //省市区三级联动数据
      citydata: regionData,
      //物流信息对话框
      expressDialogVisiable: false,
      //物流信息
      expressData:[]
    };
  },
  filters: {
    //格式化时间戳
    formatTime(time) {
      return formatTime(time * 1000, "Y-M-D h:m:s");
    },
  },
  methods: {
    //打开物流信息对话框
    showExpressBox(row) {
      getdata({
        method:'get',
        url:`/kuaidi/1106975712662`
      }).then(result => {
        const res = result.data
        if(res.meta.status !== 200) {
          //获取信息失败
          this.$message.error('获取物流信息失败')
          return ;
        }
        this.expressData = res.data
        this.expressDialogVisiable = true
      })
      
    },
    //关闭地址对话框重置数据并清空校验
    addressDialogClose() {
      this.$refs.addressFormRef.resetFields();
    },
    //监听级联选择器的数值发生变化的时间
    handleChange() {},
    //显示修改地址对话框
    showAddressBox() {
      this.addressBoxVisiable = true;
    },
    //监听当前页发生变化
    handleCurrentChange(newPageNum) {
      this.queryInfo.pagenum = newPageNum;
      this.getOrderList();
    },
    // 监听当前每一页显示的条数
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getOrderList();
    },
    // 获取订单列表
    getOrderList() {
      getdata({
        method: "get",
        url: "/orders",
        params: this.queryInfo,
      }).then((result) => {
        const res = result.data;
        if (res.meta.status !== 200) {
          //请求数据失败
          this.$message.error("获取订单列表失败");
          return;
        }
        //请求数据成功
        this.orderList = res.data.goods;
        this.total = res.data.total;
        console.log(this.orderList);
      });
    },
  },
  created() {
    this.getOrderList();
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
.el-cascader {
  width: 100%;
}
</style>